package cz.oplevka.lahvodroid;

import android.app.Activity;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.os.Bundle;
import android.os.Environment;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.io.File;
import java.io.FileInputStream;

public class XMLParsing extends Activity {

	/** Create Object For SiteList Class */
	public static XMLBeerList xmlBeerList = null;
        LinearLayout layout;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.parsertext);

		/** Create a new layout to display the view */
		layout = new LinearLayout(this);
		layout.setOrientation(1);
                layout.setScrollContainer(true);
                layout.setScrollbarFadingEnabled(true);	
                
                
                parseXML();	
                writeText();
//                layout.addView(scrollView);

		/** Set the layout view to display */
		setContentView(layout);  
                
	}

    private void writeText() {
        
        /** Get result from XMLHandler SitlesList Object */
	xmlBeerList = XMLHandler.xmlBeerList;
        
        /** Create a new textview array to display the results */
        TextView[] barcode;
        TextView[] jmeno;
        TextView[] typ;
        TextView[] pivovar;
        TextView[] alkohol;
        TextView[] poznamka;        
        
        /** Assign textview array lenght by arraylist size */
        barcode = new TextView[xmlBeerList.getBarcode().size()];
        jmeno = new TextView[xmlBeerList.getBarcode().size()];
        typ = new TextView[xmlBeerList.getBarcode().size()];
        pivovar = new TextView[xmlBeerList.getBarcode().size()];
        alkohol = new TextView[xmlBeerList.getBarcode().size()];
        poznamka = new TextView[xmlBeerList.getBarcode().size()];
        
        /** Set the result text in textview and add it to layout */
        for (int i = 0; i < xmlBeerList.getBarcode().size(); i++) {
                barcode[i] = new TextView(this);
                barcode[i].setText("Barcode = "+xmlBeerList.getBarcode().get(i));
                jmeno[i] = new TextView(this);
                jmeno[i].setText("Jmeno = "+xmlBeerList.getJmeno().get(i));
                typ[i] = new TextView(this);
                typ[i].setText("Typ = "+xmlBeerList.getTyp().get(i));
                pivovar[i] = new TextView(this);
                pivovar[i].setText("Pivovar = "+xmlBeerList.getPivovar().get(i));
                alkohol[i] = new TextView(this);
                alkohol[i].setText("Alkohol = "+xmlBeerList.getAlkohol().get(i));
                poznamka[i] = new TextView(this);
                poznamka[i].setText("Poznamka = "+xmlBeerList.getPoznamka()
                        .get(i)+"\n");

                layout.addView(barcode[i]);
                layout.addView(jmeno[i]);
                layout.addView(typ[i]);
                layout.addView(pivovar[i]);
                layout.addView(alkohol[i]);
                layout.addView(poznamka[i]);
//                layout.addView(poznamka[i]);
        }
    }

    private void parseXML() {
        try {
                
                /** Handling XML */
                SAXParserFactory spf = SAXParserFactory.newInstance();
                SAXParser sp = spf.newSAXParser();
                XMLReader xr = sp.getXMLReader();
                
                String SD_CARD_PATH = Environment.getExternalStorageDirectory().toString();
                File file = new File(SD_CARD_PATH + "/Lahvodroid/test.xml");

                /** Send URL to parse XML Tags */
//			URL sourceUrl = new URL(
//					"http://www.srandapage.webz.cz/test.xml");

                /** Create handler to handle XML Tags ( extends DefaultHandler ) */
                XMLHandler myXMLHandler = new XMLHandler();
                xr.setContentHandler(myXMLHandler);
//			xr.parse(new InputSource(sourceUrl.openStream()));
                xr.parse(new InputSource(new FileInputStream(file)));
                
        } catch (Exception e) {
                System.out.println("XML Pasing Excpetion = " + e);
        }
    }
        
//    @Override
//    public void onResume(){
//        super.onResume();
//        parseXML();	
//        writeText();
//
//	/** Set the layout view to display */
//	setContentView(layout);         
//    }
        
    /**
     * @return the xmlBeerList
     */
    public static XMLBeerList getXmlBeerList() {
        return xmlBeerList;
    }
}